/*
 * @Author: 王炜皓
 * @Date: 2023-06-14 19:25:30
 * @LastEditTime: 2023-06-15 16:29:19
 * @LastEditors: 王炜皓
 * @Description: 折线绘制
 * @FilePath: \cesium-tool\src\views\test\extend\Draw\BrokenLine\BrokenLine.ts
 */

import {
  newSessionid,
  getCatesian3FromPX,
  createRemindertip,
  destroyTip,
  hexToCesiumColor,
} from "../../plotCommon";
import EditPolyLine from "./edit";
const Cesium = window.Cesium;
interface PolygonLineDraw {
  code?: String;
  size?: String;
  style?: any;
  ground?: any;
}
export default class BrokenLine {
  _param: PolygonLineDraw;
  _entities: Array<any>;
  pickerEntity: any;
  editPolyLine: any;
  handler: any;
  changeDepth: boolean;
  _viewer: any;
  _editEntity: any;

  constructor(viewer:any) {
    this._viewer = viewer;
    this._entities = [];
    this.changeDepth = false;
    this._param = {
      code: undefined,
      size: undefined,
      style: undefined,
      ground: undefined,
    };
  }
  get options() {
    return this._param;
  }
  set options(val) {
    val = Object.assign({}, val);
    this._param = val;
    if (this._editEntity) {
      let e = this._editEntity;
      e.code = val.code;
    }
  }
  get entities() {
    return this._entities;
  }
  set entities(val) {
    this._entities = val;
  }
  create() {
    // const code=options.code||newSessionid();
    let viewer = this._viewer;
    const id = newSessionid();
    this.handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
    // const onground = options.onground || true;
    // const width=options.size||10
    // const style=options.style||Cesium.Color.White

    if (viewer.entities.getById(id))
      throw new Error("the id parameter is an unique value");
    let toolTip = "左键点击开始绘制";
    let anchorpoints: any[] = [];
    let polyline: any = undefined;
    // 左键点击事件
    this.handler.setInputAction((event: any) => {
      const { code, ground, size, style } = this._param;

      console.log(style, "color");

      let toolTip = "左键添加点，右键撤销，左键双击结束绘制";
      let pixPos = event.position;
      let cartesian = getCatesian3FromPX(viewer, pixPos);
      if (anchorpoints.length == 0) {
        anchorpoints.push(cartesian);
        polyline = viewer.entities.add({
          code: code,
          name: "Polyline",
          id: newSessionid(),
          polyline: {
            show: true,
            positions: new Cesium.CallbackProperty(function () {
              return anchorpoints;
            }, false),
            width: size,
            material: new Cesium.PolylineGlowMaterialProperty({
              glowPower: 0.3,
              color: hexToCesiumColor(style, 1),
            }),
            clampToGround: ground,
          },
        });
        polyline.GeoType = "Polyline"; //记录对象的类型，用户后续编辑等操作
        polyline.Editable = true; //代表当前对象可编辑,false状态下不可编辑
      }
      anchorpoints.push(cartesian);
      createRemindertip(toolTip, event.position, true);
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
    // 鼠标移动事件
    this.handler.setInputAction((movement: any) => {
      console.log("鼠标移动事件监测：------创建折线------");
      let endPos = movement.endPosition;
      createRemindertip(toolTip, endPos, true);
      if (Cesium.defined(polyline)) {
        anchorpoints.pop();
        let cartesian = getCatesian3FromPX(viewer, endPos);
        anchorpoints.push(cartesian);
      }
    }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
    // 左键双击事件
    this.handler.setInputAction((event: any) => {
      anchorpoints.pop();
      anchorpoints.pop(); //因为是双击结束，所以要pop两次，一次是move的结果，一次是单击结果
      polyline.PottingPoint = Cesium.clone(
        polyline.polyline.positions.getValue(),
        true
      ); //记录对象的节点数据，用户后续编辑等操作
      this.handler.destroy();
      createRemindertip(toolTip, event.position, false);
    }, Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);
    // 右键摁下事件
    this.handler.setInputAction(() => {
      anchorpoints.pop();
    }, Cesium.ScreenSpaceEventType.RIGHT_DOWN);
  }
  stopCreate() {
    destroyTip();
    this.handler.destroy();
  }
  edit(editCalback:any) {
    this.stopCreate();
    let viewer = this._viewer;
    let handler2 = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
    const calback = (e:any) => {
      if (editCalback && typeof editCalback == "function")editCalback(e)
      if (this.editPolyLine) this.editPolyLine.clear();
      this.editPolyLine = new EditPolyLine(e, viewer, handler2);
      this.editPolyLine.startEdit();
    };
    this.getPicking(handler2, viewer, calback, false);
  }
  stopEdit() {
    let viewer = this._viewer;
    destroyTip();
    if (this.handler) this.handler.destroy();
    if (this.editPolyLine) this.editPolyLine.stopEdit();
  }
  removeById(id:any) {
    this._viewer.entities.removeById(id);
    this._entities = this._entities.filter((item) => item.id != id);
  }
  getPicking(handler:any, viewer:any, callback:any, openTripParemeter:any) {
    // 创建一个ScreenSpaceEventHandler对象
    let _this = this;
    handler.setInputAction(function (click:any) {
      console.log("getPicking正在拾取坐标");

      var position = click.position;
      var pickedObject = viewer.scene.pick(position);
      // 检查pickedObject是否为实体
      if (
        Cesium.defined(pickedObject) &&
        Cesium.defined(pickedObject.id) &&
        pickedObject.id instanceof Cesium.Entity
      ) {
        // 获取点击的实体+
        var entity = pickedObject.id;
        // 输出实体信息
        const toolTip = pickedObject.id;
        _this.pickerEntity = entity;
        if (callback && typeof callback == "function") callback(entity);
        if (openTripParemeter && typeof openTripParemeter == "string")
          createRemindertip(entity[openTripParemeter], click.position, true);
      }
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
  }
}
